﻿#include "CPageModel.h"

QStringList CPageModel::mimeTypes() const
{
	return QStringList();
}

QMimeData * CPageModel::mimeData(const QModelIndexList & indexes) const
{
	QMimeData *mime = new QMimeData();

	// 获取第一个被选中的项（仅支持单项拖拽）
	if (!indexes.isEmpty()) 
	{
		QStandardItem *item = itemFromIndex(indexes.first());
		if (!item)
		{
			return mime;
		}
		QString sName = item->data(Qt::UserRole + 2).toString();
		QVariant var = item->data(Qt::UserRole+1);
		// 判断是否是我们定义的 DeviceInfo 类型
		if (var.canConvert<QVariantList>())
		{
			QVariantList varList = var.toList();
			QJsonArray array;
			for (const QVariant &v : varList) 
			{
				if (v.canConvert<SPageItem>())
				{
					SPageItem info = v.value<SPageItem>();
					QJsonObject obj;
					obj["guid"] = info.sGuid;
					obj["cardguid"] = info.sCardGuid;
					obj["row"] = info.nRow;
					obj["col"] = info.nCol;
					obj["rowspan"] = info.nRowSpan;
					obj["colspan"] = info.nColSpan;
					array.append(obj);
				}
			}
			if (!array.isEmpty()) 
			{
				QJsonDocument doc(array);
				mime->setData("application/x-pageinfo", doc.toJson());
				mime->setText(sName);
			}		
		}
	}
	return mime;
}
